<?php
namespace app\interfaces\controller;

use app\interfaces\model\Brand as BrandModel;
use think\Request;
use think\Exception;
use think\Db;
class Brand
{

    /*
     * 用户端 - 品牌街列表
     */
    public function brand_type()
    {
        $a = array (1, 2, array ("a", "b", "c"));
        var_export ($a,true);
    }

    public function a(){

        $group_create_time = Db::name('member_distribution_records')->group('create_time')->select();

        $group_order_number = Db::name('member_distribution_records')->group('shop_order_number')->select();
        $i = 0;
        foreach ($group_create_time as $k => &$v){
            foreach ($group_order_number as $k1 => $v1){
                if($v['shop_order_number'] == $v1['shop_order_number']){
                    $i++;
                }
            }
        }
        halt($i);
//
    }

    /**
     * 查询会员信息5个奖项+提现记录 余额 积分 对比
     */
    public function mem()
    {

        $member_list = Db::name('member')
            ->order('id','asc')
            ->field('id,balance,integral,y,e,f,g,gt')
            ->where('balance','gt',0)
            ->select()->toArray();
        foreach ($member_list as $k => &$v){
            $y = Db::name('member')->where('id',$v['id'])->value('y');
            $e = Db::name('member')->where('id',$v['id'])->value('e');
            $f = Db::name('member')->where('id',$v['id'])->value('f');
            $g = Db::name('member')->where('id',$v['id'])->value('g');
            $gt = Db::name('member')->where('id',$v['id'])->value('gt');
            $v['total_balance'] = ($y+$e+$f+$g+$gt);
            $v['total_balance9'] = ($y+$e+$f+$g+$gt)*0.9;
            $v['tixian'] = Db::name('member_withdraw')->where('member_id',$v['id'])->where('status',2)->field('sum(price) price')->find();
        }
        halt($member_list);

    }

    /**
     * @return array回滚订单
     */
    public function huigun(){
        halt(1);
        // 启动事务
        $boole = false;
        Db::startTrans();
        try {
            $shop_order_number = '20180823195819490588369499403248';
            $create_time = '2018-08-26 23:09:47';
            $data = Db::name('member_distribution_records')
                ->where('shop_order_number', $shop_order_number)
                ->where('create_time', $create_time)
                ->select();
        foreach ($data as $k => $v){
            dump($v['member_id']);
            dump($v['type']);
            dump($v['price']);
            $hah = Db::name('member')->where('id',$v['member_id'])->fetchSql($boole)->setDec($v['type'],$v['price']);//5个奖项恢复
            echo '-';
        }
            $sum = Db::name('member_distribution_records')
                ->where('shop_order_number', $shop_order_number)
                ->where('create_time', $create_time)
                ->group('member_id')
                ->field('member_id,sum(price) price')
                ->select();

            foreach ($sum as $k1 => $v1) {
                $price9 = bcmul($v1['price'], 0.9, 2);
                $price1 = floor(bcmul($v1['price'], 0.1, 2));
                dump($v1['member_id']);
                dump($v1['price']);
                dump($price9);
                dump($price1);
                echo '-----';//余额和积分恢复
                $a = Db::name('member')->where('id', $v1['member_id'])->fetchSql($boole)->setDec('balance', $price9);
                $b = Db::name('member')->where('id', $v1['member_id'])->fetchSql($boole)->setDec('integral', $price1);
                echo $a;
                echo $b;
            }
            $c = Db::name('member_distribution_records')
                ->where('shop_order_number', $shop_order_number)
                ->where('create_time', $create_time)
                ->fetchSql($boole)
                ->delete();
            echo $c;
            // 提交事务
            Db::commit();
        } catch (Exception $e) {
            // 回滚事务
            Db::rollback();
            return ['code' => 200 , 'message' => $e->getMessage(),'line' => $e->getLine()];
        }

    }

    /**
     *修复数据
     */

    public function tongji()
    {
        halt(1);
        $data = Db::name('member_distribution_records')->field('member_id,sum(price) sum_price,sum(price)*9/10 sum_price9,sum(price)*1/10 sum_price1')->group('member_id')->select();
        $boole = false;
        Db::startTrans();
        try {
            $member_id_list = Db::name('member_distribution_records')->field('member_id')->group('member_id')->column('member_id');
            $arr = [];
            foreach ($member_id_list as $k => $v) {
                $arr['id-' . $v]['5个独立奖'] = Db::name('member_distribution_records')->field('member_id,type,sum(price) div_price')->group('type')->where('member_id', $v)->select()->toArray();
                foreach ($arr['id-' . $v]['5个独立奖'] as $k1 => $v1) {
                    $arr['id-' . $v]['5个独立奖'][$k1]['sql'] = Db::name('member')->where('id', $v1['member_id'])->fetchSql($boole)->update([$v1['type'] => $v1['div_price']]);
                }
                $arr['id-' . $v]['余额和积分'] = $balance_integral = Db::name('member_distribution_records')->field('sum(price) sum_price,sum(price)*9/10 balance,sum(price)*1/10 integral')->group('member_id')->where('member_id', $v)->find();
                $arr['id-' . $v]['提现'] = $tixian = Db::name('member_withdraw')->field('sum(price) price')->where('status', 2)->where('member_id', $v)->find();
                $arr['id-' . $v]['剩余余额'] = $balance_integral['balance'] - $tixian['price'];
                if($arr['id-' . $v]['剩余余额'] < 0){
                    throw new Exception('提现金额大于用户余额');
                }
                //$arr['id-' . $v]['5个独立奖'][$k1]['sql'] = Db::name('member')->where('id', $v1['member_id'])->fetchSql($boole)->update([$v1['type'] => $v1['div_price']]);
                //
                dump($v);
                dump($v1['member_id']);
                //Db::name('member_order')->where('member_id',$v)->field('sum(use_integral),sum(balance)')->find();
                $arr['id-' . $v]['恢复余额'] = Db::name('member')->where('id', $v1['member_id'])->fetchSql($boole)->update(['balance' => $arr['id-' . $v]['剩余余额']]);
                //
                $arr['id-' . $v]['恢复积分'] = Db::name('member')->where('id', $v1['member_id'])->fetchSql($boole)->update(['integral' => $arr['id-' . $v]['余额和积分']['integral']]);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return ['code' => 200 , 'message' => $e->getMessage(),'line' => $e->getLine()];
        }
        halt($arr);
    }

    /*
     * 用户端 - 品牌街字母列表
     */
    public function brand_index()
    {

        // $type_id = Request::instance()->param('type_id');
        $letter = Request::instance()->param('letter','A');

        // if (empty($letter)) throw new Exception('not Empty');

        $model = new BrandModel();

        $result = $model->where(['letter' => ['in', $letter], 'shop_id' => ['neq', '']])
            ->field('shop_id,picUrl')
            ->paginate(8);

        return ['code' => 200, 'message' => '成功', 'data' => $result];

    }

}
